package com.his.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.his.admin.service.RegistrationService;
import com.his.dao.domain.Do.SelectAllRegistrationDo;
import com.his.dao.domain.dto.ListDto;
import com.his.dao.domain.dto.RegistrationDto;
import com.his.dao.domain.entity.Hospital;
import com.his.dao.domain.entity.Registration;
import com.his.dao.domain.entity.Role;
import com.his.dao.domain.vo.ListVo;
import com.his.dao.mapper.RegistrationMapper;
import com.his.util.constant.RedisConstant;
import com.his.util.result.RestResult;
import com.his.util.result.RestResultBuilder;
import com.his.util.util.MapUtil;
import com.his.util.util.StringUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedHashMap;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fall
 * @since 2022-11-28
 */
@Service
@AllArgsConstructor
public class RegistrationServiceImpl extends ServiceImpl<RegistrationMapper, Registration> implements RegistrationService {
    private RegistrationMapper registrationMapper;
    private RedisTemplate redisTemplate;

    @Override
    public RestResult all(ListDto listDto) {
        RegistrationDto registration = MapUtil.conversionMapToObj((LinkedHashMap) listDto.getObj(), RegistrationDto.class);;

        return new RestResultBuilder().success(ListVo
                .builder()
                .list(registrationMapper.selectAll(
                        registration.getDoctorId(),
                        registration.getPatientId(),
                        registration.getHospitalId(),
                        registration.getIsAppointment(),
                        registration.getIsComplete(),
                        registration.getIsPayment(),
                        registration.getStartTime(),
                        registration.getEndTime(),
                        listDto.getPage() * listDto.getPageSize(),
                        listDto.getPageSize()))
                .total(registrationMapper.selectAllCount(
                        registration.getDoctorId(),
                        registration.getPatientId(),
                        registration.getHospitalId(),
                        registration.getIsAppointment(),
                        registration.getIsComplete(),
                        registration.getIsPayment(),
                        registration.getStartTime(),
                        registration.getEndTime()))
                .build());
    }

    @Override
    public RestResult allDoctor(String authorization, ListDto listDto) {
        RegistrationDto registration = MapUtil.conversionMapToObj((LinkedHashMap) listDto.getObj(), RegistrationDto.class);
        Integer hospitalId = StringUtil.getId(RedisConstant.HOSPITAL, (String) redisTemplate.opsForValue().get(RedisConstant.getHospitalAndAuthorization(authorization)));
        Integer userId = StringUtil.getId(RedisConstant.USER, (String) redisTemplate.opsForValue().get(RedisConstant.getUserAndAuthorization(authorization)));

        return new RestResultBuilder().success(ListVo
                .builder()
                .list(registrationMapper.selectAllDoctor(
                        userId,
                        registration.getPatientId(),
                        hospitalId,
                        StringUtil.getCurrentYearMonthDay(),
                        StringUtil.getCurrentYearMonthDay(),
                        0,
                        1000))
                .total(registrationMapper.selectAllCountDoctor(
                        userId,
                        registration.getPatientId(),
                        hospitalId,
                        StringUtil.getCurrentYearMonthDay(),
                        StringUtil.getCurrentYearMonthDay()))
                .build());
    }

    @Override
    public RestResult selectLast(Registration registration) {
        SelectAllRegistrationDo selectAllRegistrationDo = registrationMapper.selectLast(
                registration.getDoctorId(),
                registration.getPatientId(),
                registration.getIsAppointment(),
                registration.getIsComplete(),
                registration.getIsPayment(),
                registration.getDate()
                );
        return new RestResultBuilder().success(selectAllRegistrationDo);
    }
}
